package com.dream.servlet;

import com.dream.util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 匠人码农
 * @date 2020/11/08 8:23
 * 概要：
 *      Cookie相关操作
 *      1.Cookie的创建
 *      2.Cookie的获取
 *      3.修改Cookie的修改
 *      4.Cookie的声明周期设定
 *        1> 默认生命周期负1（session生命周期结束，cookie删除。session默认生命周期是关闭并推出浏览器。）
 *        2> 生命周期设置为0（立即删除cookie）
 *      5.有效path
 *
 */

public class CookieServlet extends BaseServlet {
    /**
     * 1.创建Cookie
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建Cookie对象
        Cookie cookie  = new Cookie("key1","value");
        //发送cookie给客户端
        resp.addCookie(cookie);
        //输出信息到客户端
        resp.getWriter().write("创建cookie成功！");
    }

    /**
     * 2.获取Cookie
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cookie
        Cookie[] cookies = req.getCookies();

        //遍历cookies
        for(Cookie cookie : cookies){
            resp.getWriter().write("cookie[" + cookie.getName() + ":" + cookie.getValue()+"]<br/>");
        }

    }

    /**
     * 3.修改cookie的值
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cookie
        Cookie[] cookies = req.getCookies();

        //查询要修改的cookie
        Cookie cookie = CookieUtil.findCookie("key1",cookies);

        if(cookie != null){
            //创建一个同样key值的cookie,并给其新值。
            Cookie cookie2 = new Cookie("key1","newValue");
            //发送给客户端
            /*
              浏览器Set-Cookie
             */
            resp.addCookie(cookie2);

            resp.getWriter().write("Cookie[" + cookie.getName() + "]" + "已经被更新了。更新后的值：" + cookie2.getValue());
        }

    }

    /**
     * 4.Cookie的默认生命周期
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void defaultLifeCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建默认生命周期的cookie
        Cookie cookie = new Cookie("defaultLifeCookie","defaultLifeCookie");

        //设置默认生命周期
        cookie.setMaxAge(-1);

        //发送给客户端
        resp.addCookie(cookie);

        resp.getWriter().write("默认生命周期已经设定完毕！！！");

    }


    /**
     * 4.设置声明周期为立即删除（0）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteNowCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建默认生命周期的cookie
        Cookie cookie1 = new Cookie("deleteNowCookie","deleteNowCookie");

        //设置一个默认周期的Cookie和立即删除的cookie进行对比。
        Cookie cookie2 = new Cookie("defaultLifeCookie","defaultLifeCookie");

        //设置为立即删除
        cookie1.setMaxAge(0);

        //发送给客户端
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);

        resp.getWriter().write("立即生命周期已经设定完毕！！！");

    }

    /**
     * 4.设置为整数
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void life30Cookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建默认生命周期的cookie
        Cookie cookie = new Cookie("life30","life30");

        //设置为立即删除
        cookie.setMaxAge(30);

        //发送给客户端
        resp.addCookie(cookie);

        resp.getWriter().write("30秒生命周期已经设定完毕！！！");

    }

    /**
     *  path作用
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pathCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建cookie
//        Cookie cookie1 = new Cookie("cookieA","AValue");
        Cookie cookie2 = new Cookie("cookieB","BValue");

        //设置有效path为:/工程路径/abc
//        cookie1.setPath(req.getContextPath());

        cookie2.setPath(req.getContextPath() + "/abc");

        //请求地址为：http://localhost:8080/JavaWebCookieSession/abc/cookie.html 时发送，cookie
        //请求地址为：http://localhost:8080/JavaWebCookieSession/cookie.html 时不发送cookie。因为工程名—/abc不符合url地址。

        //发送给客户端
//        resp.addCookie(cookie1);
        resp.addCookie(cookie2);

        resp.getWriter().write("path已经设定完毕！！！");

    }


}
